package top.kaoshanji.t1ch10b;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.List;

/**
 * 代码清单test-t1ch10b-01：测试一下接口
 * @author kaoshanji
 * ^_^
 * create time 2025/4/30 18:45
 */
@SpringBootTest
@AutoConfigureMockMvc
public class CountryControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testFrance() throws Exception {

        Country country = new Country();
        country.setName("France");
        country.setPopulation(67);

        var obj = new ObjectMapper().writeValueAsString(country);

        mockMvc.perform(MockMvcRequestBuilders.get("/france"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().json(obj));

    }

    @Test
    public void testCountryList() throws Exception {

        Country c1 = new Country();
        c1.setName("France");
        c1.setPopulation(67);

        Country c2 = new Country();
        c2.setName("Spain");
        c2.setPopulation(47);

        var obj = new ObjectMapper().writeValueAsString(List.of(c1, c2));

        mockMvc.perform(MockMvcRequestBuilders.get("/countryList"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().json(obj));

    }


}
